//初始化目录
jQuery(function($) {
	loadTree(-1);
	$("#main_right").tabs();
});
	//$("#main_right").append("<iframe id='iframe_home' src='"+baseUrl+"/asset/learnfw/ui-manage/components.htm"+"' scrolling='auto' border='0' height='" + ($(window).height() - 105) + "px'></iframe>");
//加载 目录 ajax
function loadTree(treeId) {
 $.ajax({
		type:"post",
		url:baseUrl+'/menu/queryMenuList.do',
		data:"nodeId="+treeId,
		async:true,
		success:function(result){
			tree(result);
		}
	});
}
//生成目录 具体代码
function tree(tree){
	$.each(tree, function(i) {
		if(tree[i].leaf&&$("#tree_head_"+tree[i].id).attr('id')==undefined) {
			$("#tree_" + tree[i].parentId).append("<div id='tree_head_" + tree[i].id + "'><h3>" + tree[i].text + "</h3></div>");
			$("#hidden").append("<input type='hidden' id='url_" + tree[i].id + "' value='" +parseUrl(tree[i].url) + "'/>");
			$("#hidden").append("<input type='hidden' id='text_" + tree[i].id + "' value='" + tree[i].text + "'/>");
			$("#tree_head_" + tree[i].id).accordion({
				collapsible: true,
				active: false,
				heightStyle: 'content',
				icons:{ "header": "ui-icon-document", "activeHeader": "ui-icon-document" }
			}).click(function() {
				var oldTabsH=$("#tabs").height();
				var id_num = $(this).attr('id').substring(10);
				var li_num = $("#tabs li").length;
					if(undefined == $("#tab_" + id_num).attr('id')) {
						$("#tabs").append("<li id='tab_" + id_num + "'><a href='#iframe_" + id_num + "'>" + $("#text_" + id_num).attr('value') + "</a><a href='javascript:onCloseTab(" + id_num + ")' class='ui-icon ui-icon-close'></a></li>");
						$("#main_right").append("<iframe id='iframe_" + id_num + "' src='" + $("#url_" + id_num).attr('value') + "' scrolling='auto' border='0' height='" + ($(window).height() - 105) + "px'></iframe>");
						$("#main_right").tabs("refresh");
						$("#main_right").tabs("option", "active", li_num);
					}
				if($("#tabs").height()!=oldTabsH) {
					$("#dialog_alert").empty();
					$("#dialog_alert").append("<label>打开的页面太多,请关闭一些再打开新页面!</label>");
					$("#dialog_alert").dialog({
						resizable: false,
						modal: true,
						buttons: {
							"确定": function() {
								$(this).dialog("close");
								onCloseTab(id_num);
							}
						}
					});
				}
			});
		} else if($("#tree_head_"+tree[i].id).attr('id')==undefined){
			$("#tree_" + tree[i].parentId).append("<div id='tree_head_" + tree[i].id + "'><h3>" + tree[i].text + "</h3><div id='tree_" + tree[i].id + "'></div></div>");
			$("#tree_head_" + tree[i].id).accordion({
				collapsible: true,
				active: false,
				heightStyle: 'content',
				icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" }
			}).on("accordionbeforeactivate", function(event, ui) {
					ui.newPanel.empty();
					if(ui.newPanel.attr('id') != undefined) {
					loadTree(ui.newPanel.attr('id').substring(5));
				}
			});
		}
	});
}
//关闭 标签
function onCloseTab(tabId_num) {
	$("#tab_" + tabId_num).remove();
	$("#iframe_" + tabId_num).remove();
	$("#main_right").tabs("refresh");
	$("#tree_head_"+tabId_num+">h3").removeClass("ui-accordion-header-active ui-state-active");
	$("#tree_head_"+tabId_num+">h3").addClass("ui-accordion-header-collapsed ui-corner-all");
}
//解析 url
function parseUrl(url){
	if (null==url) {
		return null;
	}else{
		return baseUrl+"/asset/learnfw/p2p/"+url.replace('.jsp','.htm').substring(7).replace(/\//g,'.').replace(/\.query/g,'').replace(/\.main/g,'');
	}
}
//退出 登陆
function onLoginOut() {
	$("#dialog_alert").empty();
	$("#dialog_alert").append("<label>确定退出吗?</label>");
	$("#dialog_alert").dialog({
		resizable: false,
		modal: true,
		buttons: {
			"确定": function() {
				window.location.href = baseUrl + "/j_spring_security_logout";
				$(this).dialog("close");
			},
			"取消": function() {
				$(this).dialog("close");
			}
		}
	});
}
//修改密码
function changePassword() {
	$("#dialog_alert").empty();
	$("#dialog_alert").append("<label>确定修改密码吗?</label>");
	$("#dialog_alert").dialog({
		resizable: false,
		modal: true,
		buttons: {
			"确定": function() {
				$(this).dialog("close");
				$("#dialog_changePassword").dialog({
					resizable: false,
					modal: true,
					minHeight: 200,
					minWidth: 400,
					buttons: {
						"取消": function() {
							$(this).dialog("close");
						},
						"修改": function() {
							var oldPwd = $("#oldPwd").val();
							var newPwd = $("#newPwd").val();
							var confirmPwd = $("#confirmPwd").val();
							var u_alert = null;
							if('' == oldPwd) {
								u_alert = "旧密码不能为空!";
							} else if('' == newPwd) {
								u_alert = "新密码不能为空!";
							} else if('' == confirmPwd) {
								u_alert = "确认密码不能为空!";
							} else if(newPwd != confirmPwd) {
								u_alert = "新密码和确认密码输入的不一样!!";
							} else if(oldPwd == newPwd) {
								u_alert = "新密码和旧密码不能一样!";
							}
							if(null != u_alert) {
								$("#dialog_alert").empty();
								$("#dialog_alert").append("<label>" + u_alert + "</label>");
								$("#dialog_alert").dialog({
									modal: true,
									buttons: {
										"确定": function() {
											$(this).dialog("close");
										}
									}
								});
							} else {
								var str = $("#form_update").serialize();
								$(this).dialog("close");
								$.ajax({
									type: "post",
									url: baseUrl + "/p2puser/changePassword.do",
									data: str,
									success: function(result) {
										if(result.isSuccess) {
											$("#dialog_alert").empty();
											$("#dialog_alert").append("<label>修改密码成功!</label>");
											$("#dialog_alert").dialog({
												modal: true,
												buttons: {
													"确定": function() {
														$(this).dialog("close");
														window.location.href = baseUrl + "/j_spring_security_logout";
													}
												}
											});
										} else {
											$("#dialog_alert").empty();
											$("#dialog_alert").append("<label>" + result.msg + "</label>");
											$("#dialog_alert").dialog({
												modal: true,
												buttons: {
													"确定": function() {
														$(this).dialog("close");
													}
												}
											});
										}
									},
									error: function() {
										$("#dialog_alert").empty();
										$("#dialog_alert").append("<label>修改密码失败!</label>");
										$("#dialog_alert").dialog({
											modal: true,
											buttons: {
												"确定": function() {
													$(this).dialog("close");
												}
											}
										});
									}
								});
							}
						}
					}
				});

			},
			"取消": function() {
				$(this).dialog("close");
			}
		}
	});
}